/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.security.core;

import io.iec.edp.caf.security.core.details.User;
import io.iec.edp.caf.security.core.details.UserTypeEnum;

import java.util.*;

/**
 * This is {@link CurrentUserDetails}.
 *
 * @author zhangtianyi
 * @since 1.0.0
 */
public class CurrentUserDetails implements UserDetails {

    private static final long serialVersionUID = -4737116536165380190L;

    private final User user;

    private CurrentUserDetails(User user) {
        this.user = user;
    }

    public Optional<String> getNickName() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getNickName()
                : Optional.empty();
    }

    public Optional<String> getFullName() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getFullName()
                : Optional.empty();
    }

    public Optional<String> getFirstName() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getFirstName()
                : Optional.empty();
    }

    public Optional<String> getMiddleName() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getMiddleName()
                : Optional.empty();
    }

    public Optional<String> getLastName() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getLastName()
                : Optional.empty();
    }

    public Optional<String> getTitle() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getTitle()
                : Optional.empty();
    }

    public Optional<Locale> getLocale() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getLocale()
                : Optional.empty();
    }

    public Optional<String> getTimezone() {
        return user.getTimezone();
    }

    @Override
    public UserDetailsSource getUserDetailsSource() {
        return user.getUserDetailsSource();
    }


    @Override
    public boolean isAccountNonExpired() {
        return user.isCredentialsNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return user.isAccountNonLocked();

    }

    @Override
    public boolean isCredentialsNonExpired() {
        return user.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return user.isEnabled();
    }

    @Override
    public boolean isWeakPassword() {
        return (user != null
                && UserTypeEnum.PERSON.equals(user.getType()))
                && user.toPersonalUser().isWeakerPassword();
    }

    @Override
    public boolean secretMatches(String secret) {
        return (user != null
                && UserTypeEnum.PERSON.equals(user.getType()))
                && user.toPersonalUser().secretMatches(secret);
    }

    @Override
    public String getId() {
        return user != null ? user.getId() : null;
    }

    @Override
    public String getUsername() {
        return user != null ? user.getName() : null;
    }

    @Override
    public Optional<String> getFullNameEn() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getFullNameEn()
                : Optional.empty();
    }

    @Override
    public void setFullNameEn(String fullNameEn) {
        if(user != null && UserTypeEnum.PERSON.equals(user.getType()))
            user.setFullNameEn(fullNameEn);
    }


    @Override
    public Optional<String> getMail() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getMail()
                : Optional.empty();
    }

    @Override
    public Optional<String> getPhone() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getPhone()
                : Optional.empty();
    }

    @Override
    public Optional<String> getCurrentTenantId() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getCurrentTenantId()
                : Optional.empty();
    }

    @Override
    public Optional<List<String>> getTenantIds() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().getTenantIds()
                : Optional.empty();
    }

    @Override
    public boolean isBoundLocalUser() {
        return user != null && UserTypeEnum.PERSON.equals(user.getType())
                ? user.toPersonalUser().isBoundLocalUser()
                : Boolean.FALSE;
    }

    @Override
    public Optional<String> getExternalAttributeAsString(String attributeName) {
        return user.getExternalAttributeAsString(attributeName);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return user.getAuthorities();
    }

    @Override
    public void updateExternalAttribute(Map<String, String> attributes) {
        user.updateExternalAttribute(attributes);
    }

    @Override
    public User getUser() {
        return user;
    }

    public static Builder builder() {
        return new Builder();
    }


    public static class Builder {

        private User user;

        public Builder user(User user) {
            this.user = user;
            return this;
        }

        public CurrentUserDetails build() {
            return new CurrentUserDetails(user);
        }

    }
}
